package com.chenailin.www.controller;

import com.chenailin.www.common.Schedule;
import com.chenailin.www.pojo.Doctor;
import com.chenailin.www.service.DepartmentService;
import com.chenailin.www.service.DoctorService;

import java.util.List;
import java.util.Scanner;

import static com.chenailin.www.util.MenuUtil.getConfirmation;
import static com.chenailin.www.util.InputValidator.parseTimeSlots;

public class DoctorController {
    private final DoctorService doctorService = new DoctorService();
    private final DepartmentService departmentService = new DepartmentService();
    private final Scanner sc = new Scanner(System.in);

    // 添加医生
    public void addDoctor() {
        System.out.println("\n=== 添加医生 ===");
        // 1. 输入基本信息
        System.out.print("请输入医生id：");
        int id = sc.nextInt();
        sc.nextLine();
        System.out.print("请输入医生姓名：");
        String name = sc.nextLine().trim();
        System.out.print("请输入科室ID：");
        int departmentId = sc.nextInt();
        if (!departmentService.isValidDepartment(departmentId)) {
            System.out.println("错误：无效的科室ID！");
            return;
        }
        sc.nextLine();

        // 2. 选择时间段
        System.out.println("\n可用时间段列表：");
        Schedule.TIME_SLOTS.forEach((k, v) -> System.out.printf("%d. %s\n", k, v));
        System.out.print("请输入时间段编号（多个用逗号分隔）：");
        String scheduleInput = sc.nextLine();
        List<Integer> Schedule = parseTimeSlots(scheduleInput);

        // 3. 创建医生对象
        Doctor newDoctor = new Doctor(name, id, departmentId, Schedule);

        // 4. 添加是否成功
        try {
            if(doctorService.addDoctor(newDoctor)) {
                System.out.println("\n医生添加成功！");
            } else {
                System.out.println("添加失败，请检查输入数据");
            }
        } catch (Exception e) {
            System.out.println("添加失败：" + e.getMessage());
        }
    }

    // 修改医生 111
    public void modifyDoctor() {
        System.out.println("\n=== 修改医生 ===");
        System.out.print("请输入医生id：");
        int id = sc.nextInt();
        sc.nextLine();
        System.out.print("请输入新的医生姓名（留空不修改）：");
        String name = sc.nextLine();
        System.out.print("请输入新的科室ID（填0不修改）：");
        int departmentId = sc.nextInt();
        sc.nextLine();
        System.out.println("\n可用时间段列表：");
        Schedule.TIME_SLOTS.forEach((k, v) -> System.out.printf("%d. %s\n", k, v));
        System.out.print("请输入新的时间段编号（多个用逗号分隔，留空不修改）：");
        String scheduleInput = sc.nextLine();
        List<Integer> Schedule = parseTimeSlots(scheduleInput);

        // 3. 创建医生对象
        Doctor newDoctor = new Doctor(name, id, departmentId, Schedule);

        // 4. 修改是否成功
        try {
            Doctor original = doctorService.getDoctorDetail(id);

            // 构建更新对象
            Doctor updated = new Doctor();
            updated.setDocId(id);
            updated.setDocName(name.isEmpty() ? original.getDocName() : name);
            updated.setDepartmentId(departmentId == 0 ? original.getDepartmentId() : departmentId);
            updated.setSchedule(scheduleInput.isEmpty() ? original.getSchedule() : parseTimeSlots(scheduleInput));

            if(doctorService.modifyDoctor(newDoctor)) {
                System.out.println("\n医生修改成功！");
            } else {
                System.out.println("修改失败，请检查输入数据");
            }
        } catch (Exception e) {
            System.out.println("修改失败：" + e.getMessage());
        }
    }

    // 删除医生
    public void deleteDoctor() {
        System.out.println("\n=== 删除医生 ===");
        // 1. 输入基本信息
        System.out.print("请输入医生id：");
        int id = sc.nextInt();
        sc.nextLine();
        // 2. 删除是否成功
        try {
            if (!getConfirmation("确定要删除此医生吗？")) {
                return;
            }
            boolean result = doctorService.deleteDoctor(id);
            if(result) {
                System.out.println("\n医生删除成功！");
            } else {
                System.out.println("删除失败，请检查输入数据");
            }
        } catch (Exception e) {
            System.out.println("删除失败：" + e.getMessage());
        }
    }

    // 查看所有医生 可能为空
    public void listDoctors() {
        System.out.println("\n=== 医生列表 ===");
        doctorService.getAllDoctors().forEach(d -> {
            // 获取科室名称
            String departmentName = departmentService.getDepartmentDetail(d.getDepartmentId()).getDepartName();
            System.out.printf("\n%d.%s\n科室：%s\n", d.getDocId(), d.getDocName(), departmentName);
            System.out.println("排班时间段：");
            if (d.getSchedule() != null && !d.getSchedule().isEmpty()) {
                d.getSchedule().forEach(slot ->
                        System.out.printf("-%d. %s\n", slot, Schedule.getTimeBySlot(slot)));
            } else {
                System.out.println("(暂无排班信息)\n");
            }
        });
    }

    // 查看医生详情
    public void showDoctorDetail() {
        System.out.print("输入医生ID: ");
        int id = sc.nextInt();
        sc.nextLine();
        Doctor doctor = doctorService.getDoctorDetail(id);
        System.out.println("\n=== 医生详情 ===");
        System.out.println("ID: " + doctor.getDocId());
        System.out.println("姓名: " + doctor.getDocName());
        System.out.println("科室: " + doctor.getDepartmentId());
        System.out.println("排班时间段：");
        if (doctor.getSchedule() != null && !doctor.getSchedule().isEmpty()) {
            doctor.getSchedule().forEach(slot ->
                    System.out.printf("- %s\n", Schedule.getTimeBySlot(slot)));
        } else {
            System.out.println("(暂无排班信息)");
        }
    }

    // 添加时间段
    public void addSchedule() {
        System.out.println("\n=== 添加排班 ===");
        System.out.print("请输入医生id：");
        int id = sc.nextInt();
        sc.nextLine();
        Doctor doctor = doctorService.getDoctorDetail(id);
        System.out.println("\n可用时间段列表：");
        Schedule.TIME_SLOTS.forEach((k, v) ->
                System.out.printf("%d. %s\n", k, v));

        System.out.println("\n此医生的排班：");
        if (doctor.getSchedule() != null && !doctor.getSchedule().isEmpty()) {
            doctor.getSchedule().forEach(slot ->
                    System.out.printf("- %d. %s\n", slot, Schedule.getTimeBySlot(slot)));
        } else {
            System.out.println("(暂无排班信息)");
        }
        System.out.print("请输入想添加的时间段编号（多个用逗号分隔）：");
        String scheduleInput = sc.nextLine();
        List<Integer> Schedule = parseTimeSlots(scheduleInput);

        Schedule = doctorService.filterSchedule(id, Schedule);
        try {
                if(doctorService.addSchedule(id, Schedule)) {
                    System.out.println("\n排班添加成功！");
                } else {
                    System.out.println("添加失败，请检查输入数据");
                }

        } catch (Exception e) {
            System.out.println("添加失败：" + e.getMessage());
        }
    }

    // 删除时间段
    public void deleteSchedule() {
        System.out.println("\n=== 删除排班 ===");
        System.out.print("请输入医生id：");
        int id = sc.nextInt();
        sc.nextLine();
        Doctor doctor = doctorService.getDoctorDetail(id);
        System.out.println("\n可用时间段列表：");
        Schedule.TIME_SLOTS.forEach((k, v) ->
                System.out.printf("%d. %s\n", k, v));

        System.out.println("\n此医生的排班：");
        if (doctor.getSchedule() != null && !doctor.getSchedule().isEmpty()) {
            doctor.getSchedule().forEach(slot ->
                    System.out.printf("- %s\n", Schedule.getTimeBySlot(slot)));
        } else {
            System.out.println("(暂无排班信息)");
        }
        System.out.print("请输入想删除的时间段编号（多个用逗号分隔）：");
        String scheduleInput = sc.nextLine();
        List<Integer> Schedule = parseTimeSlots(scheduleInput);

        try {
            if (!getConfirmation("确定要删除此时间段吗？")) {
                return;
            }
            boolean result = doctorService.deleteSchedule(id, Schedule);
            if(result) {
                System.out.println("\n排班删除成功！");
            } else {
                System.out.println("删除失败，请检查输入数据");
            }
        } catch (Exception e) {
            System.out.println("删除失败：" + e.getMessage());
        }
    }

}
